package com.koi.easyexcel.core.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ･ᴗ･
 * @description [UploadDataListener]
 */
public class UploadDataListener<T> extends AnalysisEventListener<T> {

    // 数据集
    private final List<T> list = new ArrayList<>();

    public List<T> getList() {
        return this.list;
    }

    /**
     * @description [每条数据都会进入]
     * @author ･ᴗ･
     */
    @Override
    public void invoke(T object, AnalysisContext analysisContext) {
        this.list.add(object);
    }

    /**
     * @description [数据解析完调用]
     * @author ･ᴗ･
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * @description [异常时调用]
     * @author ･ᴗ･
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        // 数据解析异常
        if (exception instanceof ExcelDataConvertException excelDataConvertException) {
            throw new RuntimeException("第" + excelDataConvertException.getRowIndex() + "行" + excelDataConvertException.getColumnIndex() + "列" + "数据解析异常");
        }
        // 其他异常...
    }
}
